package com.haochang.chunkvod.controller.activity.order;

import com.haochang.base.BaseActivity;
import com.haochang.base.EventProxy;
import com.haochang.base.IEventConstant;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.common.intent.IntentKey;
import com.haochang.chunkvod.app.database.order.GoodsInfoDao;
import com.haochang.chunkvod.controller.fragment.order.AlreadyOrderFragment;
import com.haochang.chunkvod.controller.fragment.order.BillFragment;
import com.haochang.chunkvod.controller.fragment.order.OrderFragment;
import com.haochang.chunkvod.controller.fragment.order.ServiceAddFragment;
import com.haochang.chunkvod.controller.fragment.order.ShopCartFragment;
import com.haochang.chunkvod.controller.fragment.order.TakeOutHistoryFragment;
import com.haochang.chunkvod.model.order.GoodsInfo;
import com.haochang.chunkvod.tools.vod.event.EventObserver;
import com.haochang.chunkvod.view.vod.tab.TabSwitcher;

import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.text.TextUtils;

/**
 *@author qintao
 *created at 2016/6/7 15:35
 * 点餐主页
 */

public class OrderMainActivity extends BaseActivity implements EventObserver
{
	private FragmentTabHost tabHost;
	private TabSwitcher tabSwitcher;
	private String roomNum;
	private String roomId;
	private String billId;
	private String roomStatus;
	private int type;
	private boolean isOrderTakeOut;//是否是点餐外带
	private String charging_room_type_id;
	private Bundle bundle = new Bundle();

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		EventProxy.addEventListener(this, IEventConstant.EVENT_ORDER_SHOP_CART);
		EventProxy.addEventListener(this, IEventConstant.EVENT_DESK_ID);
		EventProxy.addEventListener(this, IEventConstant.EVENT_DESK_STATUS);
	}

	@Override
	public void receiveParam()
	{
		roomNum = getIntent().getStringExtra(IntentKey.ROOM_NUM);
		roomId = getIntent().getStringExtra(IntentKey.ROOM_ID);
		billId = getIntent().getStringExtra(IntentKey.BILL_ID);
		isOrderTakeOut = getIntent().getBooleanExtra(IntentKey.ORDER_TAKEOUT, false);
		charging_room_type_id = getIntent().getStringExtra(IntentKey.CHARGING_ROOM_TYPE_ID);
		roomStatus = getIntent().getStringExtra(IntentKey.ROOM_STATUS);
		type = getIntent().getIntExtra(IntentKey.ROOM_TYPE, 0);
	}

	@Override
	public void initData()
	{
		bundle.putBoolean(IntentKey.ORDER_TAKEOUT, isOrderTakeOut);//是否是点餐外带
		bundle.putString(IntentKey.ROOM_NUM, roomNum);//房间号
		bundle.putString(IntentKey.ROOM_ID, roomId);//房间id
		bundle.putString(IntentKey.BILL_ID, billId);//房间状态
		bundle.putString(IntentKey.CHARGING_ROOM_TYPE_ID, charging_room_type_id);//账单id
		bundle.putString(IntentKey.ROOM_STATUS, roomStatus);//房间状态
		bundle.putInt(IntentKey.ROOM_TYPE, type);//类型
		if (TextUtils.isEmpty(billId))//账单为空清除购物车
			GoodsInfoDao.getDao().deleteBuilder(GoodsInfo.class, "bill_id", billId);
	}

	@Override
	public void initViews()
	{
		setContentView(R.layout.activity_order_main);
		tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
		tabHost.setup(OrderMainActivity.this, getSupportFragmentManager(), android.R.id.tabcontent);
		tabHost.addTab(tabHost.newTabSpec("0").setIndicator("0"), OrderFragment.class, bundle);
		if (isOrderTakeOut)
		{
			tabHost.addTab(tabHost.newTabSpec("1").setIndicator("1"), TakeOutHistoryFragment.class, bundle);
			tabHost.addTab(tabHost.newTabSpec("2").setIndicator("2"), ShopCartFragment.class, bundle);
		} else
		{
			tabHost.addTab(tabHost.newTabSpec("1").setIndicator("1"), AlreadyOrderFragment.class, bundle);
			tabHost.addTab(tabHost.newTabSpec("2").setIndicator("2"), BillFragment.class, bundle);
			if (type == 0)
			{
				tabHost.addTab(tabHost.newTabSpec("3").setIndicator("3"), ServiceAddFragment.class, bundle);
				tabHost.addTab(tabHost.newTabSpec("4").setIndicator("4"), ShopCartFragment.class, bundle);
			} else
				tabHost.addTab(tabHost.newTabSpec("3").setIndicator("3"), ShopCartFragment.class, bundle);
		}
		tabHost.setCurrentTab(0);
		tabSwitcher = (TabSwitcher) findViewById(R.id.tabSwitcher);
		tabSwitcher.addTab("点餐");
		tabSwitcher.addTab("已点");
		if (isOrderTakeOut)
			tabSwitcher.addTab("购物车");
		else
		{
			tabSwitcher.addTab("账单");
			if (type == 0)
				tabSwitcher.addTab("服务");
			tabSwitcher.addTab("购物车");
		}
		tabSwitcher.setOnTabChangedListener(new TabSwitcher.OnTabChangedListener()
		{
			@Override
			public void onTabChanged(int last, int current)
			{
				tabSwitcher.setSelectedTable(current);
				tabHost.setCurrentTab(current);
			}
		});
	}

	@Override
	public void onNotify(Object sender, int eventId, Object... args)
	{
		switch (eventId)
		{
		case IEventConstant.EVENT_ORDER_SHOP_CART:
			Integer num = (Integer) args[0];
			tabSwitcher.setShopNum(num);
			break;
		case IEventConstant.EVENT_DESK_ID:
			String bill_id = (String) args[0];
			billId = bill_id;
			bundle.putString(IntentKey.BILL_ID, billId);//账单id
			break;
		case IEventConstant.EVENT_DESK_STATUS:
			String status = (String) args[0];
			roomStatus = status;
			String bill_id2 = (String) args[1];
			billId = bill_id2;
			bundle.putString(IntentKey.ROOM_STATUS, roomStatus);//房间状态
			bundle.putString(IntentKey.BILL_ID, billId);//账单状态
			break;
		default:
			break;
		}
	}

	@Override
	public void onDestroy()
	{
		EventProxy.removeEventListener(IEventConstant.EVENT_ORDER_SHOP_CART, this);
		EventProxy.removeEventListener(IEventConstant.EVENT_DESK_ID, this);
		EventProxy.removeEventListener(IEventConstant.EVENT_DESK_STATUS, this);
		if (TextUtils.isEmpty(billId))//账单为空清除购物车
			GoodsInfoDao.getDao().deleteBuilder(GoodsInfo.class, "bill_id", billId);
		super.onDestroy();
	}
}
